#!/usr/bin/env ruby

require 'rubygems'
require 'bundler/setup'

require 'tmpdir'
require 'fileutils'
require_relative 'lib/clean_config'

DEFAULT_EFI_VOLUME = '/Volumes/EFI'

if ARGV[0].nil? && !File.exists?(DEFAULT_EFI_VOLUME)
  puts "Must specify EFI volume."
  puts "ex: ./dump_efi #{DEFAULT_EFI_VOLUME}"
  exit 1
end

EFI_VOL        = File.expand_path((ARGV[0] || DEFAULT_EFI_VOLUME).strip)
PLIST_PATH     = File.join(EFI_VOL, 'EFI', 'OC', 'config.plist')
SCRIPT_PATH    = File.expand_path(File.dirname(__FILE__))
MY_EFI_PATH    = File.join(SCRIPT_PATH, '..', 'MY EFI')
NEW_PLIST_PATH = File.join(MY_EFI_PATH, 'OC', 'config.plist')

if !File.exist?(EFI_VOL)
  puts "EFI volume not found: #{ARGV[0]}"
  exit 1
end

if !File.exist?(PLIST_PATH)
  puts "Cannot find #{config.plist}. Perhaps the volume doesn't contain OpenCore?"
  exit 1
end

FileUtils.rm_rf MY_EFI_PATH
FileUtils.cp_r(File.join(EFI_VOL, 'EFI'), MY_EFI_PATH)
CleanConfig.new(NEW_PLIST_PATH).clean!
(FileUtils.rm File.join(MY_EFI_PATH, 'opencore-*.txt')) rescue nil
(FileUtils.rm_r File.join(MY_EFI_PATH, 'OC', 'ACPI', 'decompiled')) rescue nil

puts "Done!"